package com.teach.utils;

import com.teach.entity.TbPrize;
import com.teach.mapper.TbIntegrateMapper;
import com.teach.mapper.TbPrizeMapper;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class LotteryUtils {
    @Resource
    TbPrizeMapper tbPrizeMapper;
    @Resource
    RedisTemplate redisTemplate;

    public Integer BackIntegral(){
        ArrayList<Integer> arrayList = new ArrayList<>();
        List<TbPrize> prizeList = tbPrizeMapper.selectPrizeStatus();
        for (TbPrize tbPrize : prizeList) {
            for(Integer i=0;i<tbPrize.getLucky();i++){
                arrayList.add(Integer.parseInt(tbPrize.getPrize()));
            }
        }
        //打乱积分集合
        Collections.shuffle(arrayList);
        //初始化积分栈，并让用户抽中积分
        Integer integeral = (Integer) redisTemplate.boundListOps("IntList").leftPop();
        if(integeral == null || integeral == 0 || integeral<0){
            redisTemplate.boundListOps("IntList").rightPushAll(arrayList.toArray(new Integer[16000]));
        }
        return integeral;
    }
}
